var seckill={

        URL:{

        now: function(){
            return '/seckill/time/now'
        },
        exposer:function(seckillId){

            return '/seckill/'+seckillId+'/exposer';
        },
        execution: function(seckillId,md5){

            return '/seckill/'+seckillId+"/"+md5+'/execution';

        }

        },

        handleSeckillkill:function(seckillId,node){

        console.log(node);

        node.hide().html("<button class='btn btn-primary btn-lg' id='killBtn'>开始秒杀</button>");

        $.post(seckill.URL.exposer(seckillId),{},function(result){

           console.dir(result);
         if(result&& result['success']){
            var exposer = result['data'];
            if(exposer['exposed']){
                          var md5= exposer['md5'];
                 var killUrl= seckill.URL.execution(seckillId,md5);
                 console.log("killUrl==="+killUrl);

                 $("#killBtn").one('click',function(){

                    $(this).addClass("disable");
                    //

                    $.post(killUrl,{},function(result){

                             if(result&& result['success']){
                                var killResult= result['data'];
                                var state =killResult['state'];
                                var stateInfo = killResult['stateInfo'];
                                node.html('<span class="label label-success">'+stateInfo+'</span>')

                             }
                    });
                 });

                 node.show();

           }else{

              var now = exposer['now'];
              var start = exposer['start'];
              var end = exposer['end']
               seckill.countDown(seckillId,now, start,end);

           }


         }else{
            console.log("result="+result);
         }



        });


        },

         validatePhone: function(phone){
         if(phone && phone.length ==11 && !isNaN(phone)){
            return true;
         }else{
            return false;
         }

         },

         countDown:function(seckillId,nowTime,startTime,endTime){

         var seckillBox= $("#seckill-box");
         if(nowTime>endTime){

            $("#seckill-box").html("秒杀结束");
         }else if(nowTime<startTime){
            var killTime= new Date(startTime+1000);
            $("#seckill-box").countdown(killTime, function(event){

                var format= event.strftime("秒杀倒计时 ： %D天 %H时 %M分 %S秒");
                $("#seckill-box").html(format);
            }).on('finish.countdown',function(){


                seckill.handleSeckillkill(seckillId,seckillBox);

            });

         }else{

            seckill.handleSeckillkill(seckillId,seckillBox);
         }



         },

        detail:{

            init:function(params){

                var killPhone= $.cookie("killPhone");
                var startTime = params['startTime'];
                 var endTime = params['endTime'];
                 var seckillId = params['seckillId'];

                 if(!seckill.validatePhone(killPhone)){

                    $("#input_phone").show();

                    return;

                 }

                $.get(seckill.URL.now(),{},function(result){
                    console.log(result);

                    if(result&& result['success']){
                        var nowTime= result['data'];
                        seckill.countDown(seckillId,nowTime,startTime,endTime);

                    }else{
                     console.log("result"+result);
                    }



                });

            }
        }
}